import tkinter as tk
from functools import partial
from caculator import on_keypress


def create_calculator_window():
    # 创建tk窗口，设置窗口大小，和窗口标题
    window = tk.Tk()
    window.geometry("400x600")
    window.title("计算器")

    # 创建Label控件，显示输入和结果
    display = tk.Label(window, text="", font=("Helvetica", 16), height=2, width=18)
    display.grid(row=0, column=0, columnspan=4, pady=10)

    keys = [
        ("AC", on_keypress),
        ("←", on_keypress),
        ("%", on_keypress),
        ("÷", on_keypress),
        ("7", on_keypress),
        ("8", on_keypress),
        ("9", on_keypress),
        ("x", on_keypress),
        ("4", on_keypress),
        ("5", on_keypress),
        ("6", on_keypress),
        ("-", on_keypress),
        ("1", on_keypress),
        ("2", on_keypress),
        ("3", on_keypress),
        ("+", on_keypress),
        ("0", on_keypress),
        (".", on_keypress),
        ("(", on_keypress),
        (")", on_keypress),
        ("^", on_keypress),
        ("!", on_keypress),
        ("=", on_keypress),
        ("draw", on_keypress),
        ("解方程", on_keypress),
        ("BMI", on_keypress),
        ("求导", on_keypress),
        ("Seq", on_keypress)
    ]

    row = 1
    col = 0
    for key, command in keys:
        button = tk.Button(window, text=key, font=("Helvetica", 12), height=3, width=6)
        button.grid(row=row, column=col, padx=5, pady=5)
        button.configure(command=partial(command, key, display, window))
        col += 1
        if col > 3:
            col = 0
            row += 1

    # 绑定键盘事件
    window.bind("<Key>", lambda event: on_keypress(event.char, display, window))

    # 启动tk事件循环，显示窗口
    window.mainloop()


if __name__ == "__main__":
    create_calculator_window()